<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>Vue中的JS动画封装</title>
		<script type="text/javascript" src="js/vue.js" ></script>
	</head>
	<body>
		<div id="root">
			<fade :show="show">
				<div>Hello World</div>
			</fade :show="show">
        	<button @click="handleBtnClick">切换</button>
        </div>
		
		<script>
			
			Vue.component("fade", {
				props: ['show'],
				template: ` <transition @before-enter="handleBeforeEnter"
										@enter="handleEnter"
										@after-enter="handleAfterEnter">
								<slot v-if="show"></slot>
						    </transition>`,
				methods: {
					handleBtnClick: function() {
						this.show = !this.show
					},
					handleBeforeEnter: function(el) {
						el.style.color = "red"
					},
					handleEnter: function(el, done) {
						setTimeout(() => {
							el.style.color = "green"
						}, 2000)
						setTimeout(() => {
							done()
						}, 4000)
						
					},
					handleAfterEnter: function(el) {
						el.style.color = "#000"
					}
				}
			})
			
			var vm = new Vue({
				el: '#root',
				data: {
					show: true
				},
				methods: {
					handleBtnClick: function() {
						this.show = !this.show
					}
				}
			})
			
		</script>
	</body>
</html>
